package droid;

import java.awt.Color;
import java.awt.Point;
import java.io.IOException;

import robocode.*;
import robocode.util.Utils;

public class LeaderBoot extends AbstractBoot {
	int timeScan = 0;
	ScannedRobotEvent scanEvent;
	
	public void run() {

		while (true) {

			setColors(Color.orange, Color.black, Color.green);
			setBulletColor(Color.red);
			
			//turnRadarRightRadians(Double.MIN_NORMAL);
			if (timeScan++ > 1) {
				setTurnRadarRightRadians(Double.POSITIVE_INFINITY);

			}

			if (getOthers() == 0 && timeScan > 5) {
				onScannedRobot(scanEvent);
			}

			if (pertoParede()) {

				setAhead(60);

			} else {

				ahead(400);
				descrobreDirecao();
			}

			execute();
		}

	}

	public void onScannedRobot(ScannedRobotEvent e) {

		setaDadosRoboADV(e);
		scanEvent = e;
		timeScan = 0;
		posicionaRadar(e);
		
		if (!isTeammate(e.getName())) {
						
			if (distRoboAdv < 300 && distRoboAdv > 50) {
				direita = true;
				setTurnRight(angRobAdv);
			//	setAhead(distRoboAdv);
				mira(angRobAdv+getGunCharge());
				chamaBoots(enemyX, enemyY);
				fogo(distRoboAdv);

			} else if (distRoboAdv <= 50) {

				direita = false;
				setTurnLeft(angRobAdv);
				//setAhead(distRoboAdv);
				mira(angRobAdv+getGunCharge());
				chamaBoots(enemyX, enemyY);
				fogo(distRoboAdv);

				// fire(3);
			} else if (distRoboAdv >= 300 && distRoboAdv < 450) {

				direita = true;
				setTurnRight(angRobAdv);
				//setAhead(distRoboAdv);
				mira(angRobAdv+getGunCharge());
				chamaBoots(enemyX, enemyY);
				fogo(distRoboAdv);

			}
		} else {
			setTurnRadarRight(angRobAdv);
			setBack(50);
		}

	}
	
	public void posicionaRadar(ScannedRobotEvent e) {
		setTurnRadarRightRadians(Utils.normalRelativeAngle(getHeadingRadians()
				+ e.getBearingRadians() - getRadarHeadingRadians()) * 2);
	}

	public void onHitRobot(HitRobotEvent INI) {
		angRobAdv = INI.getBearing();
		
		if (!isTeammate(INI.getName())) {

			setTurnRight(INI.getBearing());
			mira(angRobAdv+getGunCharge());
			setBack(10);
			//setAhead(distRoboAdv);
			chamaBoots(enemyX, enemyY);
			setFireBullet(3);
		} else {

			setBack(50);
		}
	}

	public void onHitWall(HitWallEvent e) {
		// angRobAdv = e.getBearing();
		setBack(20);
		setAhead(10);
		setTurnLeft(90 - angRobAdv);
	}

	public void onHitByBullet(HitByBulletEvent e) {
		angRobAdv = e.getBearing();
		setTurnRight(e.getBearing());
		setAhead(30);

	}

	public void onSkippedTurn(SkippedTurnEvent e) {

		setAhead(10);

		if (direita) {

			setTurnLeft(90 - angRobAdv);
			direita = false;

		} else {

			setTurnRight(90 - angRobAdv);
			direita = true;
		}

	}

	public void onBulletHit(BulletHitEvent e) {

		// ahead(distRoboAdv);
		// fireBullet(3);
	}

	public void onWin(WinEvent e) {
		setColors(Color.black, Color.white, Color.red);
		dancinha();
	}

	public void chamaBoots(double enemyX, double enemyY) {
		try {
			
			broadcastMessage(new Pointer(enemyX, enemyY));
			
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}

}
